﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Net;
using System.Threading;

namespace QuanLyNhanVien
{
    public partial class frmDangNhap : Form
    {
        //global variables zone
        public static Socket gsckClient;
        public static object gReceivedFlag;
        public static object gReceivedFlagGetTTMoi;
        public static bool gWaitToLogin;
        public static String gstrUserName;
        public static String gstrPassWord;
        public static long glTickPerSecond = 10000000;
        public static long glConnectionAlivePeriod = 30;
        public static List<Byte[]> sendingQueue;
        //end global variables zone
        public static frmGiaoDienChinh frmGiaoDienChinh;
        public static Panel pnlChinh;
        public static MenuStrip mnChinh;
     
        private const int IDPhongKH = 1;

        private void menuStrip_SetValue(Boolean bValue)
        {

            if (mnChinh.InvokeRequired)
            {
                mnChinh.BeginInvoke(
                new MethodInvoker(
                delegate() { menuStrip_SetValue(bValue); }
                                 )
                                    );
            }
            else
            {
                lock (mnChinh)
                {
                    mnChinh.Enabled = bValue;
                }
            }
        }

        public frmDangNhap(MenuStrip mnC, Panel pnlC, frmGiaoDienChinh frmGDC)
        {
            InitializeComponent();
            frmGiaoDienChinh = frmGDC;
            pnlChinh = pnlC;
            mnChinh = mnC;
            gWaitToLogin = true;
            gReceivedFlag = new object();
            gReceivedFlagGetTTMoi = new object();
            gReceivedFlag = false;
            gReceivedFlagGetTTMoi = false;
            //mnChinh.Enabled = false;     
            sendingQueue = new List<byte[]>();
            menuStrip_SetValue(false);
        }
        
        private void DangNhap_Click(object sender, EventArgs e)
        {
            mnChinh.Enabled = true;
            pnlChinh.Controls.Clear();
        }

        private void XoaTrang_Click(object sender, EventArgs e)
        {
            textNguoiDung.Text = "";
            textMatKhau.Text = "";
            textNguoiDung.Focus();
            DateTime value = new DateTime();
            value = DateTime.Now;
            Console.WriteLine("value.ticks {0}", DateTime.Now.Ticks);
            Thread.Sleep(1000);
            Console.WriteLine("value.ticks {0}", DateTime.Now.Ticks);            
        }

         public void Delete()
        {
            //socket_Close(gsckClient);
            gsckClient.Close();
            gsckClient = null;
            mnChinh = null;
            pnlChinh = null;
        }

 /*        public int DBExecute(string iStrDBName, string strCommand) // strConmand co the la: insert, delete, update
         {
             int iPort = 0;
             IPEndPoint iep;
             byte[] abDBName;
             try
             {
                 iep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 18115);//117.6.133.202 
                 if (gsckClient == null)
                 {
                     gsckClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                     gsckClient.Connect(iep);
                 }
                 
                 gReceivedFlag = false;
                 while (!(Boolean)gReceivedFlag) ;
                 iPort = BitConverter.ToUInt16(SocketManager.gBRecvBuff, 0);
                 gsckClient.Close();
                 gsckClient = null;

                 iep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), iPort);
                 if (gsckClient == null)
                 {
                     gsckClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                     gsckClient.Connect(iep);
                 }

                 abDBName = new byte[12 + Encoding.UTF8.GetBytes(iStrDBName).Length];

                 //length
                 BitConverter.GetBytes(8 + iStrDBName.Length).CopyTo(abDBName, 0);

                 //isReadOnly
                 BitConverter.GetBytes(0).CopyTo(abDBName, 4);

                 //Reserved
                 BitConverter.GetBytes(0).CopyTo(abDBName, 8);

                 //Database Name
                 Encoding.UTF8.GetBytes(iStrDBName).CopyTo(abDBName, 12);

                 lock (frmDangNhap.sendingQueue)
                 {
                     frmDangNhap.sendingQueue.Add(abDBName);
                 }

             }
             catch
             {
                 gsckClient = null;
                 MessageBox.Show("Không thể kết nối đến server!");
                 return -1;
             }

             gReceivedFlag = false;
             while (!(Boolean)gReceivedFlag) ;
             if (BitConverter.ToInt32(SocketManager.gBRecvBuff, 0) == 4 && BitConverter.ToInt32(SocketManager.gBRecvBuff, 4) == 0)//OK
             {
                 //length
                 BitConverter.GetBytes(8 + strCommand.Length).CopyTo(abDBName, 0);

                 //Command of execution
                 BitConverter.GetBytes(2).CopyTo(abDBName, 4);

                 //Query to execute
                 Encoding.UTF8.GetBytes(strCommand).CopyTo(abDBName, 8);
                 
                 //send package to server
                 lock (frmDangNhap.sendingQueue)
                 {
                     frmDangNhap.sendingQueue.Add(abDBName);
                 }

                 gReceivedFlag = false;
                 while (!(Boolean)gReceivedFlag) ;
                 if (BitConverter.ToInt32(SocketManager.gBRecvBuff, 0) == 4 && BitConverter.ToInt32(SocketManager.gBRecvBuff, 4) == 0)//OK
                 {
                     gsckClient.Close();
                     gsckClient = null;
                     return 0;
                 }
                 else
                 {
                     gsckClient.Close();
                     gsckClient = null;
                     MessageBox.Show("Lỗi thực thi câu SQL");
                     return -1;

                 }
             }
             else
             {
                 gsckClient.Close();
                 gsckClient = null;
                 MessageBox.Show("Lỗi Database name");
                 return -1;
             }

         }

         public int DBInsert(string iStrDBName, string strQuery, string strTable, string strIDField)
         {
             int iPort = 0;
             IPEndPoint iep;
             byte[] abDBName;
             try
             {
                 iep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 18115);//117.6.133.202 
                 if (gsckClient == null)
                 {
                     gsckClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                     gsckClient.Connect(iep);
                 }

                 gReceivedFlag = false;
                 while (!(Boolean)gReceivedFlag) ;
                 iPort = BitConverter.ToUInt16(SocketManager.gBRecvBuff, 0);
                 gsckClient.Close();
                 gsckClient = null;

                 iep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), iPort);
                 if (gsckClient == null)
                 {
                     gsckClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                     gsckClient.Connect(iep);
                 }

                 abDBName = new byte[12 + Encoding.UTF8.GetBytes(iStrDBName).Length];

                 //length
                 BitConverter.GetBytes(8 + iStrDBName.Length).CopyTo(abDBName, 0);

                 //isReadOnly
                 BitConverter.GetBytes(0).CopyTo(abDBName, 4);

                 //Reserved
                 BitConverter.GetBytes(0).CopyTo(abDBName, 8);

                 //Database Name
                 Encoding.UTF8.GetBytes(iStrDBName).CopyTo(abDBName, 12);

                 lock (frmDangNhap.sendingQueue)
                 {
                     frmDangNhap.sendingQueue.Add(abDBName);
                 }

             }
             catch
             {
                 gsckClient = null;
                 MessageBox.Show("Không thể kết nối đến server!");
                 return -1;
             }

             gReceivedFlag = false;
             while (!(Boolean)gReceivedFlag) ;
             if (BitConverter.ToInt32(SocketManager.gBRecvBuff, 0) == 4 && BitConverter.ToInt32(SocketManager.gBRecvBuff, 4) != -1)//OK
             {
                 //length
                 BitConverter.GetBytes(8 + strQuery.Length + strTable.Length + strIDField.Length).CopyTo(abDBName, 0);

                 //Command of INSERT
                 BitConverter.GetBytes(4).CopyTo(abDBName, 4);

                 //Insert query
                 Encoding.UTF8.GetBytes(strQuery).CopyTo(abDBName, 8);
                 
                 //Table name
                 Encoding.UTF8.GetBytes(strTable).CopyTo(abDBName, 8 + strQuery.Length);

                 //ID-field name
                 Encoding.UTF8.GetBytes(strIDField).CopyTo(abDBName, 8 + strQuery.Length + strTable.Length);

                 //send insert command package to server
                 lock (frmDangNhap.sendingQueue)
                 {
                     frmDangNhap.sendingQueue.Add(abDBName);
                 }

                 gReceivedFlag = false;
                 while (!(Boolean)gReceivedFlag) ;
                 if (BitConverter.ToInt32(SocketManager.gBRecvBuff, 0) == 4 && BitConverter.ToInt32(SocketManager.gBRecvBuff, 4) != -1)//OK
                 {
                     gsckClient.Close();
                     gsckClient = null;
                     return (BitConverter.ToInt32(SocketManager.gBRecvBuff, 4));
                 }
                 else
                 {
                     gsckClient.Close();
                     gsckClient = null;
                     MessageBox.Show("Lỗi thực thi câu INSERT dữ liệu");
                     return -1;

                 }
             }
             else
             {
                 MessageBox.Show("Lỗi trong xảy ra trong quá trình thực thi câu SQL");
                 gsckClient.Close();
                 gsckClient = null;
                 return -1;
             }
         }


         public string DBQuery(string iStrDBName, string strQuery)
         {
             int iPort = 0;
             int iLen = 0;
             IPEndPoint iep;
             byte[] abDBName;
             try
             {
                 iep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 18115);//117.6.133.202 
                 if (gsckClient == null)
                 {
                     gsckClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                     gsckClient.Connect(iep);
                 }

                 gReceivedFlag = false;
                 while (!(Boolean)gReceivedFlag) ;
                 iPort = BitConverter.ToUInt16(SocketManager.gBRecvBuff, 0);
                 gsckClient.Close();
                 gsckClient = null;

                 iep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), iPort);
                 if (gsckClient == null)
                 {
                     gsckClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                     gsckClient.Connect(iep);
                 }

                 abDBName = new byte[12 + Encoding.UTF8.GetBytes(iStrDBName).Length];

                 //length
                 BitConverter.GetBytes(8 + iStrDBName.Length).CopyTo(abDBName, 0);

                 //isReadOnly
                 BitConverter.GetBytes(0).CopyTo(abDBName, 4);

                 //Reserved
                 BitConverter.GetBytes(0).CopyTo(abDBName, 8);

                 //Database Name
                 Encoding.UTF8.GetBytes(iStrDBName).CopyTo(abDBName, 12);

                 lock (frmDangNhap.sendingQueue)
                 {
                     frmDangNhap.sendingQueue.Add(abDBName);
                 }

             }
             catch
             {
                 gsckClient = null;
                 MessageBox.Show("Không thể kết nối đến server!");
                 return null;
             }

             gReceivedFlag = false;
             while (!(Boolean)gReceivedFlag) ;
             if (BitConverter.ToInt32(SocketManager.gBRecvBuff, 0) == 4 && BitConverter.ToInt32(SocketManager.gBRecvBuff, 4) == 0)//OK
             {
                 //length
                 BitConverter.GetBytes(8 + strQuery.Length).CopyTo(abDBName, 0);

                 //Command of SELECT
                 BitConverter.GetBytes(3).CopyTo(abDBName, 4);

                 //Query to execute
                 Encoding.UTF8.GetBytes(strQuery).CopyTo(abDBName, 8);

                 //send package to server
                 lock (frmDangNhap.sendingQueue)
                 {
                     frmDangNhap.sendingQueue.Add(abDBName);
                 }

                 gReceivedFlag = false;
                 while (!(Boolean)gReceivedFlag) ;
                 if (BitConverter.ToInt32(SocketManager.gBRecvBuff, 4) == 0)//OK
                 {
                     iLen = BitConverter.ToInt32(SocketManager.gBRecvBuff, 0);
                     gsckClient.Close();
                     gsckClient = null;
                     return (BitConverter.ToString(SocketManager.gBRecvBuff, 8, iLen - 4));
                 }
                 else
                 {
                     gsckClient.Close();
                     gsckClient = null;
                     MessageBox.Show("Lỗi thực thi câu SELECT Query");
                     return null;

                 }
             }
             else
             {
                 gsckClient.Close();
                 gsckClient = null;
                 MessageBox.Show("Lỗi Database name không đúng!");
                 return null;
             }
         }
        */
        /*Base64 conversion for all the text, memo need to be saved into database on server*/
         /*private void ConvertBtn_Click(object sender, EventArgs e)
         {
             Base64Text.Text = System.Convert.ToBase64String(Encoding.UTF8.GetBytes(rawText.Text));
         }

         private void DecodeBtn_Click(object sender, EventArgs e)
         {
             rawText.Text = System.Text.ASCIIEncoding.ASCII.GetString(System.Convert.FromBase64String(Base64Text.Text));
         }*/

    }
}

